<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="组织" prop="orgname" required>
        <el-input
          v-model="queryParams.orgname"
          placeholder="请输入组织名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="员工号" prop="userCode">
        <el-input
          v-model="queryParams.userCode"
          placeholder="请输入员工号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="typeCode">
        <el-select v-model="queryParams.typeCode" placeholder="请选择类型">
            <el-option
              v-for="dict in dict.type.staff_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="结果类型" prop="phone">
        <el-input
          clearable
          v-model="queryParams.resultType"
          placeholder="请输入结果类型"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="培训时间" prop="orderTime">
        <el-date-picker
          :clearable="false"
          size="small"
          v-model="queryParams.beginTime2"
          type="datetime"
          format="yyyy-MM-dd hh:mm:ss"
          value-format="yyyy-MM-dd hh:mm:ss"
          placeholder="选择开始时间"
          
        >
        </el-date-picker>
        <el-date-picker
          :clearable="false"
          size="small"
          v-model="queryParams.endTime2"
          type="datetime"
          format="yyyy-MM-dd hh:mm:ss"
          value-format="yyyy-MM-dd hh:mm:ss"
          placeholder="选择结束时间"
        >
        </el-date-picker>
      </el-form-item>



      <!-- <el-form-item label="联系电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学历" prop="education">
        <el-input
          v-model="queryParams.education"
          placeholder="请输入学历"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="专业" prop="professional">
        <el-input
          v-model="queryParams.professional"
          placeholder="请输入专业"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="岗位" prop="post">
        <el-input
          v-model="queryParams.post"
          placeholder="请输入岗位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      
      <el-form-item label="删除标志" prop="dr">
        <el-input
          v-model="queryParams.dr"
          placeholder="请输入删除标志"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:staffSafeinfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:staffSafeinfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:staffSafeinfo:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:staffSafeinfo:export']"
        >花名册导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExportCard"
          v-hasPermi="['system:staffSafeinfo:exportCard']"
        >档案卡片导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="id" align="center" prop="id" /> -->
      <el-table-column label="组织" align="center" prop="orgname" />
      <el-table-column label="姓名" align="center" prop="username" />
      <el-table-column label="员工号" align="center" prop="userCode" />
      <el-table-column label="入职时间" align="center" prop="jointime" />
      <el-table-column label="类型" align="center" prop="typeCode">
        <template slot-scope="scope" >
          <dict-tag :options="dict.type.staff_type" :value="scope.row.typeCode" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="联系电话" align="center" prop="phone" />
      <el-table-column label="学历" align="center" prop="education" />
      <el-table-column label="专业" align="center" prop="professional" />
      <el-table-column label="岗位" align="center" prop="post" />
      <el-table-column label="删除标志" align="center" prop="dr" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:staffSafeinfo:query']"
          >修改</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:staffSafeinfo:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改培训档案人员信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="组织" prop="orgname">
          <el-input v-model="form.orgname" :disabled="true" />
        </el-form-item>
        <el-form-item label="姓名" prop="username">
          <el-input v-model="form.username" :disabled="true" />
        </el-form-item>
        <el-form-item label="员工号" prop="userCode">
          <el-input v-model="form.userCode" :disabled="true" />
        </el-form-item>
        <el-form-item label="类型" prop="typeCode">
          <el-select v-model="form.typeCode" placeholder="请输入类型" :clearable='false'>
          <el-option
          v-for="dict in dict.type.staff_type"
          :key="dict.value"
          :label="dict.label"
          :value="dict.value"
          />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="学历" prop="education">
          <el-input v-model="form.education" placeholder="请输入学历" />
        </el-form-item>
        <el-form-item label="专业" prop="professional">
          <el-input v-model="form.professional" placeholder="请输入专业" />
        </el-form-item>
        <el-form-item label="岗位" prop="post">
          <el-input v-model="form.post" placeholder="请输入岗位" />
        </el-form-item>
        <el-form-item label="删除标志" prop="dr">
          <el-input v-model="form.dr" placeholder="请输入删除标志" />
        </el-form-item> -->
        <!-- <el-divider content-position="center">类型</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddStaffType">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteStaffType">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="staffTypeList" :row-class-name="rowStaffTypeIndex" @selection-change="handleStaffTypeSelectionChange" ref="staffType">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="员工id" prop="userid" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.userid" placeholder="请输入员工id" />
            </template>
          </el-table-column>
          <el-table-column label="员工类型(0管理-1普通-2特种)" prop="typename" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.typename" placeholder="请输入员工类型(0管理-1普通-2特种)" />
            </template>
          </el-table-column>
        </el-table> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-hasPermi="['system:staffSafeinfo:edit']">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo, exportWord } from "@/api/staffSafe/info";

export default {
  name: "Info",
  dicts: ['staff_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedStaffType: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 培训档案人员信息表格数据
      infoList: [],
      // 培训档案人员类型表格数据
      staffTypeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orgname: '新乡市瑞丰新材料股份有限公司',
        username: null,
        phone: null,
        education: null,
        professional: null,
        post: null,
        endTime: null,
        userCode: null,
        typeCode: '1',
        dr: null,
        resultType: null,
        beginTime2: new Date(Date.now()-(1000*60*60*24*365)),
        endTime2: new Date()
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询培训档案人员信息列表 */
    getList() {
       if (!this.queryParams.orgname || this.queryParams.orgname.trim() === '') {
        return 'orgname cannot be empty';
        }
      this.queryParams.endTime=this.queryParams.endTime2+'';
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orgname: null,
        username: null,
        phone: null,
        education: null,
        professional: null,
        post: null,
        userCode: null,
        typeCode: null
      };
      this.staffTypeList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加培训档案人员信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInfo(id).then(response => {
        this.form = response.data;
        this.staffTypeList = response.data.staffTypeList;
        this.open = true;
        this.title = "修改培训档案人员信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.staffTypeList = this.staffTypeList;
          if (this.form.id != null) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除培训档案人员信息编号为"' + ids + '"的数据项？').then(function() {
        return delInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 培训档案人员类型序号 */
    rowStaffTypeIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 培训档案人员类型添加按钮操作 */
    handleAddStaffType() {
      let obj = {};
      obj.userid = "";
      obj.typename = "";
      obj.typecode = "";
      obj.dr = "";
      obj.userCode = "";
      this.staffTypeList.push(obj);
    },
    /** 培训档案人员类型删除按钮操作 */
    handleDeleteStaffType() {
      if (this.checkedStaffType.length == 0) {
        this.$modal.msgError("请先选择要删除的培训档案人员类型数据");
      } else {
        const staffTypeList = this.staffTypeList;
        const checkedStaffType = this.checkedStaffType;
        this.staffTypeList = staffTypeList.filter(function(item) {
          return checkedStaffType.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleStaffTypeSelectionChange(selection) {
      this.checkedStaffType = selection.map(item => item.index)
    },
     /** 导出按钮操作 */
    handleExport() {
      if (!this.queryParams.orgname || this.queryParams.orgname.trim() === '') {
        return 'orgname cannot be empty';
      }
      this.download('system/staffSafeinfo/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    },
    /** 导出卡片按钮操作 */
    handleExportCard() {
      if (!this.queryParams.orgname || this.queryParams.orgname.trim() === '') {
        return 'orgname cannot be empty';
      }
      this.download('system/staffSafeinfo/exportCard', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.docx`)
    
    }

  }
};
</script>
